﻿using System;

namespace HAOest.Time
{
    /// <summary>
    /// 获取倒计时剩余时间
    /// <para>作者：邓斌HADB</para>
    /// <para>修订：2012-01-11 V0.1</para>
    /// <para>修订：2012-09-28 V1.0 从旧项目移植并修改了适用于.NET3.0的方法</para>
    /// </summary>
    public class GetRemainingTime
    {
        /// <summary> 
        /// 获取剩余时间（输入截止时间）
        /// <para>1、若剩余时间大于一天，则返回“**天”；</para>
        /// <para>2、若剩余时间不足一天，则返回“**小时**分钟**秒；</para>
        /// <para>3、若超时，则返回“TIMEOUT”</para>
        /// </summary>
        /// <param name="year">截止时间的年份</param>
        /// <param name="month">截止时间的月份</param>
        /// <param name="day">截止时间的天</param>
        /// <param name="hour">截止时间的小时</param>
        /// <param name="minute">截止时间的分钟</param>
        /// <param name="second">截止时间的秒</param>     
        public string Deadline(int year, int month, int day, int hour, int minute, int second)
        {
            DateTime deadlineTime = new DateTime(year, month, day, hour, minute, second);
            TimeSpan remainingTime = deadlineTime.Subtract(GetNowTime.Default());
            double remainingDays = remainingTime.TotalDays;
            if (remainingDays > 1)
                return (remainingTime.Days.ToString() + "天");
            else if (remainingDays > 0)
                return (remainingTime.Hours + "小时" + remainingTime.Minutes + "分钟" + remainingTime.Seconds + "秒");
            else return ("TIMEOUT");
        }

        /// <summary>
        /// 获取剩余时间（输入开始时间和倒计时总时间）
        /// <para>1、若还有时间，返回“**小时**分钟**秒”；</para>
        /// <para>2、若超时，则返回“TIMEOUT”</para>
        /// </summary>
        /// <param name="startTime">开始时间（在登录成功时定义一个DateTime startTime = DateTime.Now; 然后将startTime传入即可）</param>
        /// <param name="hours">倒计时的小时数</param>
        /// <param name="minutes">倒计时分钟数</param>
        public string Limited(DateTime startTime, int hours, int minutes)
        {
            TimeSpan totalTime = new TimeSpan(0, hours, minutes, 0);//总剩余时间
            TimeSpan remainingTime = totalTime.Subtract(GetNowTime.Default().Subtract(startTime));//还剩余时间（总剩余时间-已使用时间）
            if (remainingTime.TotalSeconds > 0)
                return (remainingTime.Hours + "小时" + remainingTime.Minutes + "分钟" + remainingTime.Seconds + "秒");
            else return ("TIMEOUT");
        }
    }
}
